<?php
/**
 * 用户中心
 */
class UserController extends AppController {
	
	/**
	 * 初始化执行
	 *
	 * @see AppController::beforeFilter()
	 */
	public function beforeFilter() {
		parent::beforeFilter ();
		
		// 加载css文件
		$this->set ( 'cssList', array (
				'user.css',
				'userpage.css' 
		) );
		
		if (is_null ( $this->Cookie->read ( 'uid' ) ) || $this->Cookie->read ( 'uid' ) == 0) {
			$this->redirect ( '/' );
		}
	}
	public function index() {
		
		// 登陆用户信息
		$this->loadModel ( "User" );
		$UserInfo = $this->User->GetUserByID ( $this->Cookie->read ( 'uid' ) );
		
		$this->set ( 'title_for_layout', "{$UserInfo['User']['nickname']}的首页--同{$UserInfo['User']['nickname']}一起分享生活发现!" );
	}
	/**
	 * 用户设置
	 */
	public function profile() {
		$this->set ( 'title_for_layout', '女人集市 -  用户设置 ' );
	}
	/**
	 * 联络信息
	 */
	public function address() {
		$this->set ( 'title_for_layout', '女人集市 -  联络信息 ' );
	}
	/**
	 * 头像
	 */
	public function headimg() {
		$this->set ( 'title_for_layout', '女人集市 -  头像 ' );
	}
	/**
	 * 邮箱
	 */
	public function email() {
		$this->set ( 'title_for_layout', '女人集市 -  邮箱 ' );
	}
	/**
	 * 修改密码
	 */
	public function passwd() {
		if ($this->request->isPost ()) {
			$password = $this->data ['password'];
			if (empty ( $password )) {
				$this->flash ( '请输入原密码!', '/user/passwd/', 1250 );
				return false;
			}
			
			// 验证原密码是否正确
			$this->loadModel ( "User" );
			$UserInfo = $this->User->GetUserByID ( $this->Cookie->read ( 'uid' ) );
			if ($UserInfo ['User'] ['password'] != md5 ( $password )) {
				$this->flash ( '原密码错误!', '/user/passwd/', 1250 );
				return false;
			}
			
			$newpassword = $this->data ['newpassword'];
			if (empty ( $newpassword )) {
				$this->flash ( '请输入新密码!', '/user/passwd/', 1250 );
				return false;
			}
			$confirmnewpassword = $this->data ['confirmnewpassword'];
			if (empty ( $confirmnewpassword )) {
				$this->flash ( '请输入确认新密码!', '/user/passwd/', 1250 );
				return false;
			}
			
			if ($newpassword != $confirmnewpassword) {
				$this->flash ( '请确认新密码是否正确!', '/user/passwd/', 1250 );
				return false;
			}
			
			$data ['User'] ['password'] = md5 ( $confirmnewpassword );
			$data ['User'] ['uid'] = $this->Cookie->read ( 'uid' );
			
			if ($this->User->edit ( $data )) {
				$this->flash ( '密码修改成功!', '/user/passwd/', 1250 );
				return false;
			} else {
				$this->flash ( '密码修改失败!', '/user/passwd/', 1250 );
				return false;
			}
		} else {
			$this->set ( 'title_for_layout', '女人集市 - 修改密码' );
			
			// 加载js文件
			$this->set ( 'scriptList', array (
					'setting.js' 
			) );
		}
	}
	/**
	 * 绑定帐号
	 */
	public function bind() {
		$this->set ( 'title_for_layout', '女人集市 -  绑定帐号 ' );
	}
	/**
	 * 邮件订阅
	 */
	public function subscription() {
		$this->set ( 'title_for_layout', '女人集市 -  邮件订阅 ' );
	}
	/**
	 * 寻找好友
	 */
	public function comein() {
		$this->set ( 'title_for_layout', '女人集市 -  寻找好友 ' );
		
		// 加载css文件
		$this->set ( 'cssList', array (
				'invite.css' 
		) );
		
		// 加载js文件
		$this->set ( 'scriptList', array (
				'comein.js' 
		) );
	}
}
?>